<%@ page language="java" pageEncoding="utf-8"%>
<%@ page contentType="text/html;charset=utf-8"%> 
<%@ page errorPage="error.jsp"%>
<%@ page import="net.sf.jasperreports.engine.*"%>
<%@ page import="net.sf.jasperreports.engine.util.*"%>
<%@ page import="net.sf.jasperreports.engine.export.*"%>
<%@ page import="net.sf.jasperreports.j2ee.servlets.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page import="org.paradise.dms.pojo.Apartment"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
	
			
	String apartmentname="";
	if (null != request.getParameter("apartmentname")) {
		if(null != request.getParameter("method") ){
			apartmentname = new String(request.getParameter("apartmentname").getBytes("ISO-8859-1"),"utf-8");
			//apartmentname = (String)(request.getParameter("apartmentname"));
		}
		else apartmentname = request.getParameter("apartmentname");
	}
	
			 
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/jquery-latest.pack.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/noConflictOfJquery.js'></script>
		<script type="text/javascript">
		
		
		$j(function(){
			var aptname = $j('#apartmentname').get(0);
			for(var a = 0;a<aptname.options.length;a++){
				if(aptname.options[a].value == '<% out.print(apartmentname);%>' )
					aptname.options[a].selected = "selected";
			}
		});
		//同步两个name="apartmentname" 的元素
		function sychronize(){
			$j('#apartmentname_h').get(0).value =  $j('#apartmentname').get(0).options[$j('#apartmentname').get(0).selectedIndex].value;
		}
	
		</script>
	</head>
	<body>
		<form action="findApartmentsForBlankbed_apartment.action" method="post"> 
			<br>楼宇号 
			<select name="apartmentname" id="apartmentname" onchange="sychronize()">
			<%
			List<Apartment> aptList = (List<Apartment>)request.getAttribute("aptList");
			for(int a = 0 ;a<aptList.size();a++){
				out.println("<option value=\"" + aptList.get(a).getApartmentname() + "\">");
				out.println(aptList.get(a).getApartmentname());
				out.println("</option>");
			}
			
			
			 %>
			</select>
			<input type="submit" name="sub" value="生成报表" />
		</form>
		<form action="jasper/getReport.action" enctype="multipart/form-data"
			method="POST">
			<input type="hidden" value="blankbed_apartment" name="reportname" />
			<input type="hidden" value="空床位统计表(按公寓)" name="downloadFileName" />
			<input type="hidden" value="<%=apartmentname%>" name="apartmentname" id="apartmentname_h"/>
			<input type="submit" value="导出excel" name="sub" />
		</form>
		<%
		
			
			
			String apmname = "";
			if (null != request.getParameter("apartmentname")) {
				if(null != request.getParameter("method") ){
					apmname = new String(request.getParameter("apartmentname").getBytes("ISO-8859-1"),"utf-8");
					//apmname = (String)(request.getParameter("apartmentname"));
				}
				else apmname = request.getParameter("apartmentname");
			}
			else
				return;
		%>
		<%@ include file="connectdb.jsp"%>
		<%

			File reportFile = new File(application
					.getRealPath("/reports/blankbed_apartment.jasper"));
			if (!reportFile.exists())
				throw new JRRuntimeException(
						"File sheet1.jasper not found. The report design must be compiled first.");

			JasperReport jasperReport = (JasperReport) JRLoader
					.loadObject(reportFile.getPath());

			Map parameters = new HashMap();
			parameters.put("ReportTitle", "Address Report");
			parameters.put("BaseDir", reportFile.getParentFile());
			parameters.put("apmname", apmname);

			JasperPrint jasperPrint = JasperFillManager.fillReport(
					jasperReport, parameters, conn);

			JRHtmlExporter exporter = new JRHtmlExporter();

			session.setAttribute(
					ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,
					jasperPrint);

			exporter
					.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
			exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
			exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,
					"../servlets/image?image=");

			exporter.exportReport();
			conn.close();
		%>
	</body>
</html>

